package com.puzzlebazar.client.admin;

import com.google.gwt.user.client.Window;
import com.puzzlebazar.client.admin.AdminGeneralView.SettingsCallback;
import com.puzzlebazar.client.main.control.TabbedSubstate;
import com.puzzlebazar.client.main.view.TabbedView;
import com.puzzlebazar.client.util.AsyncCallbackLock;
import com.puzzlebazar.client.util.ConnectionLock;


public class AdminGeneralSubstate extends TabbedSubstate {

	public static final String TAB_NAME = "General";
	public static final String STATE_NAME = "General";

	private final MySettingsCallback settingsCallback = new MySettingsCallback();
	private ConnectionLock savingLock = new ConnectionLock("Saving...");
	private ConnectionLock resettingLock = new ConnectionLock("Resetting...");
	
	private final SiteModel model;

	private AdminGeneralView adminGeneralView = null;
	
	public AdminGeneralSubstate(SiteModel model, TabbedView view, AdminController controller) {
		super(view, controller);
		
		this.model = model;
		savingLock.setGlobalLock(true);
		resettingLock.setGlobalLock(true);
	}

	@Override
	public String getTabName() {
		return TAB_NAME;
	}

	@Override
	public String getStateName() {
		return STATE_NAME;
	}

	@Override
	public void enter() {
		super.enter();

		if( adminGeneralView == null )
			adminGeneralView = new AdminGeneralView();

		adminGeneralView.resetState();
		
		view.setMainWidget( adminGeneralView );

		enableCallbacks();	
	}

	@Override
	public void exit() {
		super.exit();
		disableCallbacks();
	}

	public class MySettingsCallback implements SettingsCallback {

		@Override
		public void cancel() {
			controller.backToPreviousState();
		}

		@Override
		public void apply(SiteInfo siteInfo) {

			model.modifySiteInfo( siteInfo, 
					new AsyncCallbackLock<Boolean>(savingLock) {
						@Override
						public void onFailure(Throwable caught) {
							super.onFailure(caught);
							Window.alert( "Connection to the server has failed" );
						}
						@Override
						public void onSuccess(Boolean result) {
							super.onSuccess(result);
							if( result ){
								controller.setConfirmStateChange( null );
								controller.forwardToPreviousState();
							}
							else {
								if( !result )
									Window.alert( "An unknown error has occured, try modifying the settings again later." );
							}
						}
			} );
		}

		@Override
		public void modified( boolean isDifferent ) {
			if( isDifferent )
				controller.setConfirmStateChange( "Discard modifications?" );
			else
				controller.setConfirmStateChange( null );
		}

		@Override
		public void reset() {
			model.resetDatabase( resettingLock );
		}

	}
	

	/**
	 * Turns all callbacks on
	 */
	private void enableCallbacks() {
		adminGeneralView.addCallback( settingsCallback );
	}

	/**
	 * Turns all defaults callbacks off
	 */
	private void disableCallbacks() {
		adminGeneralView.removeCallback( settingsCallback );
	}

	@Override
	public void release() {
		super.release();
		disableCallbacks();
	}

	
}
